package tk.heimashuma.store.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.heimashuma.store.entity.Commodity;
import tk.heimashuma.store.entity.CommodityType;
import tk.heimashuma.store.entity.dto.EsCommoditySearch;
import tk.heimashuma.store.entity.es.EsCommodity;
import tk.heimashuma.store.entity.vo.PagingVo;
import tk.heimashuma.store.service.CommodityService;
import tk.heimashuma.store.utils.HttpRequestUtil;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Controller
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 用于主页的商品显示
     * @param pagingVo
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/list")
    public @ResponseBody PagingVo commodityAllList(PagingVo pagingVo) throws InterruptedException {
        if( Optional.ofNullable(pagingVo).isPresent() || pagingVo.getCount() == -1){
             pagingVo = commodityService.countCommodityNum(pagingVo);
        }
        pagingVo = commodityService.commodityAllList(pagingVo);
        pagingVo.getCommodities();
        return pagingVo;
    }
    /*@GetMapping("/list/{keyWorld}")
    public @ResponseBody List<EsCommodity> esCommodityListMath( @PathVariable String keyWorld){
        List<EsCommodity> esCommodities = commodityService.commodityListByEs(keyWorld);
        return esCommodities;
    }*/
    @GetMapping("/commodity_info/{id}")
    public String getCommodity_info(@PathVariable Integer id, Map<String,Object> map){
        if(!Optional.ofNullable(id).isPresent()){
            return "redirect:/index";
        }

        Optional<Commodity> commodityOpt = commodityService.findCommodity_infoById(id);
        if(!commodityOpt.isPresent()){
            return "redirect:/index";
        }

        map.put("info",commodityOpt.get());
        return "/commodity/commodity_info";
    }
    @PostMapping("/commodity_type")
    public @ResponseBody List<CommodityType> getCommodity_type(@NotNull String type){
        if("first".equals(type)){
            List<CommodityType> firstList = commodityService.findCommodityTypeFirst();
            return firstList;
        }else{
            List<CommodityType> child = commodityService.findCommodityTypeChildByName(type);
            if(Optional.ofNullable(child).isPresent()){
                return child;
            }
            return null;
        }

    }
    @GetMapping(value = {"/list/{keyWorld}"})
    public String getCommodityList(@PathVariable String keyWorld,Map<String,Object> map,Integer type){
        EsCommoditySearch esCommoditySearch = searchKeyword(keyWorld, 1, 20,type);
        map.put("commodity",esCommoditySearch);
        return "/commodity/gouwu";
    }
    @GetMapping("/keyword/{word}")
    public @ResponseBody Set<String> keyWord(@PathVariable String word){
        Set<String> set = HttpRequestUtil.searchKey(word);
        return set;
    }
    /**
     * 分页
     */
    @GetMapping("/search_keyword/{page}/{size}/{keyword}")
    public
    @ResponseBody
    EsCommoditySearch
    searchKeyword(@PathVariable String keyword, @PathVariable Integer page, @PathVariable Integer size,Integer type){

        // 将搜索的词添加到Redis的数据库
        if(!"".equals(keyword.trim()))
            commodityService.saveHotWordToRedis(keyword);
        page -= 1;
        if("list".equals(keyword)){
            keyword = "";
        }
        AggregatedPage<EsCommodity> esCommodities = null;
        if(type == null){
            esCommodities = commodityService.commodityListSearchByEs(keyword, page, size);
        }else{
            esCommodities = commodityService.commodityListSearchByEs(keyword, page, size,type);
        }

        EsCommoditySearch esCommoditySearch = new EsCommoditySearch();
        if(Optional.ofNullable(esCommodities).isPresent()){
            esCommoditySearch.setEsCommodityList(esCommodities.getContent());
            esCommoditySearch.setCode(200);
            esCommoditySearch.setMsg("ok");
        }else{
            esCommoditySearch.setCode(501);
            esCommoditySearch.setMsg("no data");
        }
        return esCommoditySearch;
    }

    @GetMapping("/findAllType")
    public @ResponseBody
    List<CommodityType> getAllCommodityType(){
        List<CommodityType> commodityTypes = commodityService.getAllCommodityType();

        return commodityTypes;
    }
    @GetMapping("/list")
    public String  toList(Map<String,Object> map,Integer type){
        EsCommoditySearch esCommoditySearch = searchKeyword("", 1, 20,type);
        map.put("commodity",esCommoditySearch);
        return "/commodity/gouwu";
    }
    @GetMapping("/getHotWord")
    public @ResponseBody Set<String> getHotWord(){
        Set<String> hotWordByRedis = commodityService.getHotWordByRedis();
        return hotWordByRedis;
    }

}
